using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;

namespace Macaw.Moss2007.PublishingScheduleFieldControlAdapterSolution
{
    class PublishingScheduleFieldControlAdapterFeatureReceiver : SPFeatureReceiver
    {
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
            if (webApplication == null) return;

            PublishingScheduleFieldControlAdapterJobDefinition job = (PublishingScheduleFieldControlAdapterJobDefinition) webApplication.JobDefinitions[PublishingScheduleFieldControlAdapterJobDefinition.JobId];
            if (job != null)
            {
                job.Delete();
            }

            job = new PublishingScheduleFieldControlAdapterJobDefinition(PublishingScheduleFieldControlAdapterJobDefinition.JobName, webApplication);
            job.Installing = true;
            job.Schedule = new SPOneTimeSchedule(DateTime.Now);
            job.Update();

        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWebApplication webApplication = properties.Feature.Parent as SPWebApplication;
            if (webApplication == null) return;

            PublishingScheduleFieldControlAdapterJobDefinition job = (PublishingScheduleFieldControlAdapterJobDefinition)webApplication.JobDefinitions[PublishingScheduleFieldControlAdapterJobDefinition.JobId];
            if (job != null)
            {
                job.Delete();
            }

            job = new PublishingScheduleFieldControlAdapterJobDefinition(PublishingScheduleFieldControlAdapterJobDefinition.JobName, webApplication);
            job.Installing = false;
            job.Schedule = new SPOneTimeSchedule(DateTime.Now);
            job.Update();
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            
        }
    }
}
